package com.alibaba.excel.convert;

import com.alibaba.excel.converters.Converter;
import com.alibaba.excel.metadata.GlobalConfiguration;
import com.alibaba.excel.metadata.data.ReadCellData;
import com.alibaba.excel.metadata.data.WriteCellData;
import com.alibaba.excel.metadata.property.ExcelContentProperty;
import com.alibaba.excel.util.StringUtils;
import com.alibaba.excel.util.TypeUtils;
import lombok.AllArgsConstructor;
import org.ehcache.impl.internal.classes.commonslang.ArrayUtils;

import java.lang.reflect.Field;

/**
 * @author lxk
 * @date 2022/1/18
 * @description
 */
@AllArgsConstructor
public class ExcelExpConvert implements Converter<Object> {

    private String[] converterExp;


    /**
     * excel转java
     *
     * @param cellData
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public Object convertToJavaData(ReadCellData cellData, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        String stringValue;
        switch (cellData.getType()) {
            case BOOLEAN:
                if (cellData.getBooleanValue() == null) {
                    return null;
                }
                stringValue = cellData.getBooleanValue().toString();
                break;
            case NUMBER:
                if (cellData.getNumberValue() == null) {
                    return null;
                }
                stringValue = cellData.getNumberValue().toString();
                break;
            default:
                stringValue = cellData.getStringValue();
                if (StringUtils.isBlank(stringValue)) {
                    return null;
                }
        }
        Field field = contentProperty.getField();
        if (!ArrayUtils.isEmpty(converterExp)) {
            for (String group : converterExp) {
                if (StringUtils.isNotBlank(group)) {
                    String[] map = group.split("=");
                    if (map[1].equals(stringValue)) {
                        return TypeUtils.cast(map[0], field.getType());
                    }
                    if (map[0].equals(stringValue)) {
                        return TypeUtils.cast(map[1], field.getType());
                    }
                }
            }
        }
        return TypeUtils.cast(stringValue, field.getType());
    }

    /**
     * java转excel
     *
     * @param value
     * @param contentProperty
     * @param globalConfiguration
     * @return
     * @throws Exception
     */
    @Override
    public WriteCellData<?> convertToExcelData(Object value, ExcelContentProperty contentProperty, GlobalConfiguration globalConfiguration) throws Exception {
        if (value == null) {
            return new WriteCellData();
        }
        String[] excelMappings = converterExp;
        if (!ArrayUtils.isEmpty(excelMappings)) {
            for (String group : excelMappings) {
                if (StringUtils.isNotBlank(group)) {
                    String[] map = group.split("=");
                    if (map[0].equals(value.toString())) {
                        return new WriteCellData<>(map[1]);
                    }
                    if (map[1].equals(value.toString())) {
                        return new WriteCellData<>(map[0]);
                    }
                }
            }
        }
        return new WriteCellData(value.toString());
    }


}
